

records = [
    {
        "studentNo": 1,
        "name": "嬴政",
        "age": 52,
        "sex": "男",
        "class": "2班",
        "card": "1000200030004000",
        "city": "上海"
    },
    {
        "studentNo": 2,
        "name": "韩非子",
        "age": 29,
        "sex": "女",
        "class": "3班",
        "card": "1000200030003000",
        "city": "天津"
    },
    {
        "studentNo": 3,
        "name": "王剪",
        "age": 27,
        "sex": "女",
        "class": "4班",
        "card": "1000200030002000",
        "city": "河南"
    },
    {
        "studentNo": 4,
        "name": "刘邦",
        "age": 53,
        "sex": "男",
        "class": "5班",
        "card": "1000200030004000",
        "city": "北京"
    },
    {
        "studentNo": 5,
        "name": "韩信",
        "age": 32,
        "sex": "男",
        "class": "1班",
        "card": None,
        "city": "河北"
    },
    {
        "studentNo": 6,
        "name": "项宇",
        "age": 30,
        "sex": "女",
        "class": "5班",
        "card": "1000200030005000",
        "city": "天津"
    },
    {
        "studentNo": 7,
        "name": "李红广",
        "age": 40,
        "sex": "男",
        "class": "5班",
        "card": "1000200030008000",
        "city": "上海"
    },
    {
        "studentNo": 8,
        "name": "刘彻",
        "age": 39,
        "sex": "男",
        "class": "2班",
        "card": "1000200030007000",
        "city": "广州"
    },
    {
        "studentNo": 9,
        "name": "韩王",
        "age": 28,
        "sex": "女",
        "class": "6班",
        "card": None,
        "city": "上海"
    },
    {
        "studentNo": 10,
        "name": "韩大夫",
        "age": 21,
        "sex": "女",
        "class": "7班",
        "card": "1000200030009000",
        "city": "山东"
    },
    {
        "studentNo": 11,
        "name": "霍去病",
        "age": 29,
        "sex": "男",
        "class": "8班",
        "card": "1000200020004000",
        "city": "广州"
    },
    {
        "studentNo": 12,
        "name": "萧何",
        "age": 36,
        "sex": "男",
        "class": "3班",
        "card": "1000200030006000",
        "city": "上海"
    },
    {
        "studentNo": 13,
        "name": "卫青",
        "age": 37,
        "sex": "女",
        "class": "8班",
        "card": "1000200060004000",
        "city": "重庆"
    },
    {
        "studentNo": 14,
        "name": "曹总",
        "age": 26,
        "sex": "女",
        "class": "3班",
        "card": "1000200080004000",
        "city": "四川"
    },
    {
        "studentNo": 15,
        "name": "王韩亮",
        "age": 56,
        "sex": "女",
        "class": "2班",
        "card": None,
        "city": "云南"
    },
    {
        "studentNo": 16,
        "name": "刘备",
        "age": 51,
        "sex": "男",
        "class": "5班",
        "card": "1000100030004000",
        "city": "北京"
    },
    {
        "studentNo": 17,
        "name": "薛刚",
        "age": 28,
        "sex": "男",
        "class": "4班",
        "card": "1000100030005000",
        "city": "河南"
    },
    {
        "studentNo": 18,
        "name": "柳红",
        "age": 20,
        "sex": "男",
        "class": "4班",
        "card": "1000100030005000",
        "city": "广州"
    },
    {
        "studentNo": 19,
        "name": "陈胜",
        "age": 25,
        "sex": "男",
        "class": "1班",
        "card": "1000100030006000",
        "city": "北京"
    },
    {
        "studentNo": 20,
        "name": "刘韩",
        "age": 29,
        "sex": "男",
        "class": "1班",
        "card": "1000100030007000",
        "city": "上海"
    },
    {
        "studentNo": 21,
        "name": "霍去病",
        "age": 40,
        "sex": "男",
        "class": "2班",
        "card": "1000200030005010",
        "city": "海南"
    },
    {
        "studentNo": 22,
        "name": "项宇",
        "age": 20,
        "sex": "女",
        "class": "2班",
        "card": "1000200030006000",
        "city": "天津"
    },
    {
        "studentNo": 23,
        "name": "八戒",
        "age": 25,
        "sex": "男",
        "class": "2版",
        "card": "100085",
        "city": "上海"
    },
    {
        "studentNo": 24,
        "name": "韩正",
        "age": 35,
        "sex": "男",
        "class": "3班",
        "card": "1000200030005011",
        "city": "哈尔滨"
    },
    {
        "studentNo": 25,
        "name": "孙悟空",
        "age": 39,
        "sex": "男",
        "class": "6班",
        "card": "1010102323343",
        "city": "北京"
    }
]


# 定义默认返回结果
result = {'code':0,'message':'登录成功!'}


# 需求 ：
"""
1. 支持登录 ，只有登录成功才能进行其它操作 : 登录函数
2. 支持增删改查 ：Student
3. 支持用户操作 ：选择以上的功能
4. 5个功能中成功后添加一个时间 ，比如新增时间，修改时间
5. 如果想要删除多条数据 ，支持多条删除 。
6. 将上所数据都存放在数据库中
"""

# 实现的过程 :
"""
1. 理思路
2 .写代码
"""

# 实现登录函数
def login(username,password):

    # 用户名为空或密码为空 ，给出提示
    if username is None or username == '':
        return {'code':1,'message':'用户名不能为空！'}
    if password is None or password == '':
        return {'code':2,'message':'密码不能为空！'}

    # 用户名和密码匹配登录成功
    if 'admin' == username and '123456' == password:
        Students.is_login = 0
        return result

    # 用户名和密码不匹配的情况
    return {'code':3,'message':'用户名或密码错误!'}


# 创建学生类
class Students(object):

    is_login = 1  # 0 代表是登录 ，1 代表未登录


    # 新增学生
    def add_students(self,studentNO,name,**kwargs):
        user_dict = {}
        # 1. 判断是否登录
        if Students.is_login:
            return {'code':11,'message':'用户未登录，请先进行登录!'}
        # 2. 是否输入必填项
        if studentNO is None or studentNO == '':
            return {"code":13,"message":"学生编号不能为空!"}
        if name is None or name == '':
            return {"code":14,"message":"学生姓名不能为空!"}
        sex = kwargs.get('sex')
        if sex not in ('男','女'):
            return {"code":15,"message":"性别输入不正确"}
        age = kwargs.get('age')
        if not str(age).isdigit():
            return {"code":16,"message":"输入的年龄类型不正确"}
        if not 0 <= int(age) <= 120:
            return {"code":17,"message":"输入的年龄不正确"}

        # 将用户传进来的数据，存放在字典中 。
        user_dict['studentNo'] = studentNO
        user_dict['name'] = name
        user_dict.update(**kwargs)
        # 添加到学生列表中
        records.append(user_dict)
        return {'code':21,'message':'添加学生成功!'}

    # 修改学生
    def update_students(self,studentNo,**kwargs):
        # 1. 判断是否登录
        if Students.is_login:
            return {'code': 11, 'message': '用户未登录，请先进行登录!'}
        if not str(studentNo).isdigit():
            return {'code':18,"message":"学生编号必须是整数"}
        studentNo = int(studentNo)
        # 2. 循环去数据中查找此学生ID，若找到，编辑此数据 ，没有找到返回提示
        for x in records:
            db_id = x.get('studentNo')
            if studentNo == db_id:
                x.update(**kwargs)
                return {"code":23,"message":"编辑成功"}
        return {"code":15,"message":"没有找到编辑的数据"}



    # 删除学生
    def delete_students(self,studentNo):
        if Students.is_login:
            return {'code': 11, 'message': '用户未登录，请先进行登录!'}
        if studentNo is None or studentNo == '':
            return {"code":13,"message":"学生编号不能为空!"}
        if not str(studentNo).isdigit():
            return {'code': 18, "message": "学生编号必须是整数"}
        studentNo = int(studentNo)
        # 2. 循环去数据中查找此学生ID，若找到，编辑此数据 ，没有找到返回提示
        for x in records:
            db_id = x.get('studentNo')
            if studentNo == db_id:
                records.remove(x)
                return {"code":24,"message":"删除学生记录成功!"}
        return {"code":19,"message":"没有找到要删除的学生记录"}


    # 查询学生
    def get_students(self,name):
        # 1. 判断是否登录 ，若登录就可以继续走 ，反之，返回提示
        if Students.is_login:
            return {'code':11,'message':'用户未登录，请先进行登录!'}
        # 2. 是否输入用户名 ，若没有输入，代表查询所有数据
        if name is None or name == '':
            return records

        # 3. 若输入用户名 ，若查询到数据，给返回 ，若没有查到给提示 。
        lst = []
        for x in records:
            db_name = x.get('name')
            if name == db_name:
                lst.append(x)

        # 若查询到数据则返回
        if lst:
            dct = {'code':'20','message':'查询学生成功！'}
            dct.update({'student_records':lst})
            return dct
        return {'code':'12','message':'未查到任何学生记录!'}



if __name__ == '__main__':

    flag = True
    student = Students()
    while flag:
        print("=" * 30)
        # 用户输入不同的数代表的操作
        vls = input("请用户输入对应编号进行操作:"
              "\n 1:) 进行登录:"
              "\n 2:) 新增学生:"
              "\n 3:) 编辑学生:"
              "\n 4:) 删除学生:"
              "\n 5:) 查询学生:"
              "\n q:) 退出操作:"
              "\n 其它字符:) 位置操作，请重新输入:")

        if not vls in ('1','2','3','4','5','q','quit'):
            continue

        # 登录
        if vls == '1':
            username = input("请输入用户名:")
            password = input("请输入密码:")
            login_result = login(username,password)
            print(login_result)

        # 新增
        if vls == '2':
            student_no = input("请输入学生编号:")
            s_name = input("请输入学生姓名:")
            age = input("请输入学生年龄:")
            sex = input("请输入学生性别:")
            s_class = input("请输入学生班级:")
            card = input("请输入学生身份证:")
            city = input("请输入学生城市:")
            add_result = student.add_students(studentNO=student_no,name=s_name,
                                 age=age,sex=sex,s_class=s_class,card=card,city=city)
            print(add_result)



        # 编辑
        if vls == '3':
            student_no = input("请输入学生id:")
            s_name = input("请输入学生姓名:")
            age = input("请输入学生年龄:")
            sex = input("请输入学生性别:")
            s_class = input("请输入学生班级:")
            card = input("请输入学生身份证:")
            city = input("请输入学生城市:")
            user_dict = {}
            if s_name != '':
                user_dict['name'] = s_name
            if age != '':
                user_dict['age'] = age
            if sex != '':
                user_dict['sex'] = sex
            if s_class != '':
                user_dict['class'] = s_class
            if card != '':
                user_dict['card'] = card
            if city != '':
                user_dict['city'] = city
            edit_result = student.update_students(studentNo=student_no,**user_dict)
            print(edit_result)

        # 删除
        if vls == '4':
            s_id = input("请输入删除的用户编号:")
            del_reslut = student.delete_students(s_id)
            print(del_reslut)

        # 查询
        if vls == '5':
            name = input("请输入查询的用户名:")
            print(student.get_students(name))


        # 退出
        if vls in ('q','quit'):
            flag = False
            print("退出成功")